###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# ControlPanel/Output/InstallPackage.pm.pl -> InstallPackage module           #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package Skin::CP::InstallPackage;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Standard.pm.pl";

use HTML::Dialog;
use strict;

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless ($self, $class);
}

sub DESTROY { }

###############################################################################
# show subroutine
sub show {
  my $self = shift;
  my %in = ("error" => "", @_);

  my $LANGUAGE = {
    "MISSING" => {
      "PACKAGE" => qq~<li>You didn't fill in the "Package" field.~
    },
    "INVALID" => {
      "PACKAGE" => qq~<li>The Package you specified was invalid.~
    },
    "ERROR"         => qq~<p><font class="error">There were errors:<ul>[%error%]</ul></font>~
  };

  $in{'error'} = &Standard::ProcessError(LANGUAGE => $LANGUAGE, ERROR => $in{'error'});

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Dialog = HTML::Dialog->new();

  my $Body = $Dialog->TableRow(items => [{ body => "<font color=\"Red\">Never install a package from an unverified source and always be careful when using packages that aren't directly from PlasmaPulse Solutions.</font>".$in{'error'}, colspan => 2 }]);

  $Body .= $Dialog->FormFileBox(
    name      => "FORM_PACKAGE",
    subject   => "Package",
    required  => 1
  );

  $Body .= $Dialog->FormButton(
    items   => [
      { type => "submit", value => "Next >" },
      { type => "reset", value => "Cancel" }
    ], join => "&nbsp;"
  );

  $Body = $Dialog->Table(body => $Body);
  $Body = $Dialog->StandardPage(
    body      => $Body,
    header    => "Install Package",
    form      => 1,
    multipart => 1,
    hiddens   => [
      { name => "action", value => "ViewInstallPackage" },
      { name => "CP", value => "1" },
      { name => "SID", value => $SL::ADMIN{'SID'} }
    ]
  );
  
  return $Body;
}

###############################################################################
# view subroutine
sub view {
  my $self = shift;
  my %in = ("input" => {}, @_);

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Dialog = HTML::Dialog->new();

  my ($Body, @Hiddens);
  foreach my $field (@{ $in{'input'}->{'FIELDS'} }) {
    $field->[1]->{'name'} = "FORM_".$field->[1]->{'name'} if ($field->[1]->{'name'});
    if ($field->[0] eq "textbox") {
      $Body .= $Dialog->FormTextBox(%{ $field->[1] });
    } elsif ($field->[0] eq "textarea") {
      $Body .= $Dialog->FormTextArea(%{ $field->[1] });
    } elsif ($field->[0] eq "radio") {
      $Body .= $Dialog->FormRadio(%{ $field->[1] });
    } elsif ($field->[0] eq "checkbox") {
      $Body .= $Dialog->FormCheckBox(%{ $field->[1] });
    } elsif ($field->[0] eq "selectbox") {
      $Body .= $Dialog->FormSelectBox(%{ $field->[1] });
    } elsif ($field->[0] eq "row") {
      $Body .= $Dialog->TableRow(items => [$field->[1]]);
    }
  }
  
  $Body .= $Dialog->FormButton(
    items => [
      { type => "submit", value => "Submit" },
      { type => "reset", value => "Cancel" }
    ], join => "&nbsp;"
  );

  $Body = $Dialog->Table(body => $Body);
  $Body = $Dialog->StandardPage(
    body    => $Body,
    header  => "Install Package",
    form    => 1,
    hiddens => [
      { name => "action", value => "DoInstallPackage" },
      { name => "CP", value => "1" },
      { name => "SID", value => $SL::ADMIN{'SID'} },
      @Hiddens
    ]
  );
  
  return $Body;
}

###############################################################################
# do subroutine
sub do {
  my $self = shift;
  my ($section) = @_;

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Dialog = HTML::Dialog->new();

  my $Body  = $Dialog->TableRow(items => [{ body => "Installing Package:", colspan => 2 }]);
     $Body .= $Dialog->TableRow(items => [{ body => "<iframe src=\"".$self->myURL(action => "DoInstallPackage", do => "1")."\" name=\"LOG\" height=\"300\" width=\"100%\"></iframe>", colspan => 2 }]);

  $Body .= $Dialog->FormButton(
    items => [
      { type => "button", value => "Close", extra => { onClick => "window.location='".($SL::QUERY{'Referer'} || "$SYSTEM->{'SCRIPT_URL'}?CP=1&action=Summary&SID=$SL::ADMIN{'SID'}")."';" } }
    ]
  );
  
  $Body = $Dialog->Table(body => $Body);
  $Body = $Dialog->StandardPage(
    body        => $Body,
    header      => "Install Package",
    body_extra  => "onLoad=\"document.LOG.scrollTo(0, 9999999);\""
  );
  
  return $Body;
}

###############################################################################
# myURL subroutine
sub myURL {
  my $self = shift;
  my %in = (@_);
  
  my %Params;
  
  foreach my $key ($SL::CGI->param()) {
    $Params{ $key } = [$SL::CGI->param($key)];
  }
  
  foreach my $key (keys %in) {
    if (ref($in{$key}) eq "ARRAY") {
      $Params{ $key } = $in{$key};
    } else {
      if ($in{$key} eq "") {
        delete($Params{ $key });
      } else {
        $Params{ $key } = [$in{$key}];
      }
    }
  }
  
  my @Return;
  foreach my $key (keys %Params) {
    foreach my $value (@{ $Params{$key} }) {
      push(@Return, $key."=".$SL::CGI->escape($value));
    }
  }
  
  return $SYSTEM->{'SCRIPT_URL'}."?".join("&", @Return);
}

1;